package com.zgh.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zgh.domain.BbsComment;
import com.zgh.domain.BlogComment;
import com.zgh.domain.User;
import com.zgh.mapper.BbsCommentMapper;
import com.zgh.mapper.BbsMapper;
import com.zgh.mapper.BlogCommentMapper;
import com.zgh.mapper.BlogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
@Service
public class BlogCommentService {
    @Autowired
    private BlogCommentMapper blogCommentMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private BlogMapper blogMapper;

    /**
     * 发评论
     * @param comment 评论内容
     * @param openId  openId
     * @param blogId 博客id
     * @return i
     */
    public int sendComment(String comment, String openId, String blogId) {
        //根据openId查询用户
        User user = userService.findByOpenId(openId);
        BlogComment blogComment = new BlogComment();
        blogComment.setBlogId(Integer.parseInt(blogId));
        blogComment.setUserId(user.getId());
        blogComment.setComment(comment);
        blogComment.setFid(0);
        blogComment.setThumbCount(0);
        blogComment.setCommentTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        //插入数据
        int count = blogCommentMapper.insert(blogComment);
        //查询评论数
        int comments = blogMapper.selectComments(blogId);
        //更新评论数
        blogMapper.updateComments(comments+1,blogId);
        return count;
    }

    /**
     * 点赞
     * @param id 评论id
     * @param count 点赞数
     */
    public void thumb(String id, String count) {
        UpdateWrapper<BlogComment> blogCommentUpdateWrapper = new UpdateWrapper<>();
        BlogComment blogComment = blogCommentMapper.selectById(id);
        System.out.println(blogComment);
        blogCommentUpdateWrapper.eq("id",id).set("thumbCount",count);
        blogCommentMapper.update(blogComment,blogCommentUpdateWrapper);
    }
}
